import * as cc from "cc";
import { GlobalConfig, PlayerChannel } from "../../../../GlobalConfig";
import { Loger } from "../../../../framework/loger/Loger";
import ViewModelBase from "../../../../framework/view/ViewModelBase";
import { WxUserInfo, WxUtils } from "../../../../framework/wx/WxUtils";
import { ResPlayerLogin } from "../../../../shared/protocols/PtlPlayerLogin";
import ModelPlayer from "../../player/model/ModelPlayer";
import ModelMainDemo from "../../demo_main/model/ModelMainDemo";
import { ConfigMgr } from "../../../../framework/manager/ConfigMgr";

export default class ModelLogin extends ViewModelBase {
    static modelName: string = "ModelLogin";

    initData(data: any) {

    }

    tryEnter(params?: { [key: string]: any, hideOther?: boolean, unHideNames?: string[] }) {
        if (!this.checkEnable()) {
            return;
        }
    }

    checkEnable() {
        return true;
    }

    loginServer(id?: string): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            if (WxUtils.isWx()) {
                WxUtils.login().then((code: string) => {
                    WxUtils.postMessage({
                        type: 'intData'
                    });
                    if (!!code) {
                        this.getUserInfo(code).then((userInfo: WxUserInfo) => {
                            this.callApi("PlayerLogin", {
                                code: code,
                                loginData: {
                                    nickName: userInfo.nickName,
                                    platform: cc.sys.platform,
                                    avatarUrl: userInfo.avatarUrl,
                                    channel: GlobalConfig.clientChannel
                                }
                            }).then(res => {
                                
                                if (res.isSucc) {
                                    this.syncLoginDataFromServer(res.res);
                                    resolve();
                                }
                            }).catch((err) => {

                            })
                            
                        })
                    }
                });
            } else {
                this.callApi("PlayerLogin", {
                    openId: id,
                    loginData: {
                        nickName: `游客_${id}`,
                        platform: cc.sys.platform,
                        avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJscZKCqoLCoKyTiaGh7d2hja0cYrXGhGbZicWn7rLcscbB9gpCwS5TTqnyVftzctzQHMzk5QNcticdA/132",
                        channel: PlayerChannel.Debug
                    }
                }).then(res => {
                    Loger.log(res);
                    if (res.isSucc) {
                        this.syncLoginDataFromServer(res.res);
                        resolve();
                    }
                }).catch((err) => {

                })
            }
        })
    }

    /**同步登录数据 */
    syncLoginDataFromServer(data:ResPlayerLogin){

    }

    getUserInfo(code: string) {
        return WxUtils.getUserInfo(code);
    }

    loadConfig() {
        return ConfigMgr.getInstance().loadConfig()
    }

    openNext() {
        ModelMainDemo.getInstance().tryEnter();
    }

    setTestOpenId(id:string){
        this.setLocalData("TestOpenId", id);
    }

    getTestOpenId(){
        return this.getLocalData("TestOpenId","");
    }
}
